<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('投资企业logo')" prop="sourceLogo">
            <UploadAvatar :logo-info="form.sourceLogo" @appendData="uploadSourceLogo($event)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司logo')" prop="targetLogo">
            <UploadAvatar :logo-info="form.targetLogo" @appendData="uploadTargetLogo($event)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资企业名称')" prop="sourceName">
            <el-input v-model="form.sourceName" :placeholder="$t('投资企业名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('被投资企业名称')" prop="targetName">
            <el-input v-model="form.targetName" :placeholder="$t('被投资企业名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资企业简称')" prop="alias">
            <el-input v-model="form.alias" :placeholder="$t('简称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('被投资企业简称')" prop="targetAlias">
            <el-input v-model="form.targetAlias" :placeholder="$t('被投资企业简称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资时间')" prop="investDate">
            <DatePicker
              v-model="form.investDate"
              :placeholder="$t('投资时间')"
            />

          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资比例(%)')" prop="percentage">
            <el-input v-model="form.percentage" :placeholder="$t('投资比例(%)')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="gp:businessEvent" />
    </div>
  </div>
</template>

<script>
import { saveGpBusinessEvent, getGpBusinessEvent, uploadAvatar } from '@/api/fund/gp/tycInvest/gpBusinessEvent'
import UploadAvatar from '@/components/Avatar/uploadAvatar'

export default {
  components: {
    UploadAvatar
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        sourceName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        targetName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      if (data != null && data.id !== undefined) {
        getGpBusinessEvent(data.id).then(response => {
          this.form = response.data
        })
      } else {
        this.reset()
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        gpId: this.$route.query.gpId,
        targetName: undefined,
        targetAlias: undefined,
        targetLogo: undefined,
        alias: undefined,
        investDate: undefined,
        targetId: undefined,
        sourceId: undefined,
        sourceName: undefined,
        sourceLogo: undefined,
        percentage: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      this.$parent.$parent.formOpen = false
      // 刷新list页面
      this.$parent.$parent.getList()
    },
    // 上传公司logo图片
    uploadTargetLogo(val) {
      const formData = new FormData()
      formData.append('avatarfile', val)
      formData.append('id', this.form.id)
      formData.append('type', 'targetLogo')
      uploadAvatar(formData).then(response => {
        if (response.code === 200) {
          this.form.targetLogo = response.msg
          this.msgSuccess(this.$t('上传成功'))
        } else {
          this.msgError(response.msg)
        }
      })
    },
    // 上传投资企业logo图片
    uploadSourceLogo(val) {
      const formData = new FormData()
      formData.append('avatarfile', val)
      formData.append('id', this.form.id)
      formData.append('type', 'sourceLogo')
      uploadAvatar(formData).then(response => {
        if (response.code === 200) {
          this.form.sourceLogo = response.msg
          this.msgSuccess(this.$t('上传成功'))
        } else {
          this.msgError(response.msg)
        }
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.form.source = '1'
          saveGpBusinessEvent(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    }

  }

}
</script>
